package com.yanqu.road.dao.impl.activity.businessdepart;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.businessdepart.BusinessDepartDailyActive;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BusinessDepartDailyActiveDaoImpl extends TempDataObjectDao<BusinessDepartDailyActive> {

    public static String selectSql = "select * from t_u_business_depart_daily_active where userId = ?";
    public Map<Integer, Map<String, BusinessDepartDailyActive>> getData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, Map<String, BusinessDepartDailyActive>> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusinessDepartDailyActive data = parse(rs);
                Map<String, BusinessDepartDailyActive> dataMap = map.computeIfAbsent(data.getActivityId(), k -> new ConcurrentHashMap<>());
                dataMap.put(data.getDate(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String insertSql = "INSERT INTO `t_u_business_depart_daily_active`(`userId`, `activityId`, `date`, `active`, `createTime`) VALUES (?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(BusinessDepartDailyActive data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getDate()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getActive()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_business_depart_daily_active` SET `active` = ? WHERE `userId` = ? AND `activityId` = ? AND `date` = ?";
    @Override
    protected boolean updateData(BusinessDepartDailyActive data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getActive()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getDate()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusinessDepartDailyActive parseData(ResultSet rs) throws SQLException {
        BusinessDepartDailyActive data = new BusinessDepartDailyActive();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setDate(rs.getString("date"));
        data.setActive(rs.getLong("active"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
